/**
 *
 */
package name.yzhu.wbdata.weibo;

import java.util.concurrent.BlockingQueue;

import name.yzhu.common.conf.PropertyConfig;
import name.yzhu.common.yaml.YamlItem;

import weibo4j.Oauth;
import weibo4j.Users;
import weibo4j.model.User;

/**
 * @author yhzhu
 *
 */
public class WeiboSDKFactory {
	private static final String APP_KEY = "app.key";

	private static final String APP_SECRET = "app.secret";

	private String appKey;

	private String appSecret;

	public void open(PropertyConfig proCFG){
		appKey = proCFG.getString(APP_KEY);
		appSecret = proCFG.getString(APP_SECRET);
	}

	/**
	 * @param appKey
	 * @param appSecret
	 */
	public void open(String appKey, String appSecret) {
		this.appKey = appKey;
		this.appSecret = appSecret;
	}

	public void close() {}

	public String getAccessToken() throws WeiboException {
		String state = "";//args[0];
		String scope = "email,friendships_groups_read,friendships_groups_write,statuses_to_me_read,follow_app_official_microblog";//args[1];

		Oauth oauth = new Oauth();
		try {
			oauth.authorize("code", state, scope);
		} catch (weibo4j.model.WeiboException e) {
			throw new WeiboException();
		}

		return null;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public User getUser(String token, Long uid) throws WeiboException{
		Users um = new Users();
		um.client.setToken(token);
		User user;
		try {
			user = um.showUserById(uid.toString());
		} catch (weibo4j.model.WeiboException e) {
			throw new WeiboException(e);
		}
		return user;
	}

	public User getUser(String token, String screenName) throws WeiboException{
		Users um = new Users();
		um.client.setToken(token);
		User user;
		try {
			user = um.showUserByScreenName(screenName);
		} catch (weibo4j.model.WeiboException e) {
			throw new WeiboException(e);
		}
		return user;
	}

	static public AbstractWeiboSDK getWeiboSDK(WeiboType type, BlockingQueue<YamlItem> queue) throws WeiboException {
		switch (type) {
		case SINA:
			return new SinaWeiboSDK(queue);
		case TENCENT:
			return new TencentWeiboSDK(queue);
		case SOHU:
			return new SohuWeiboSDK(queue);
		default:
			throw new WeiboException("Invalid weibo type "+type.getType());
		}
	}
}
